#ifndef refCount_h
#define refCount_h

#include "ldp/Noncopyable.h"

namespace LDP {

// help class  for reducing template hoisting 
class refCountBase {
public:
	void ref()
	{
		++m_refCount;
	}

	bool hasRef() const
	{
		return m_refCount == 1;
	}

	int refCount() const
	{
		return m_refCount;
	}
	
protected:
	refCountBase():m_refCount(1)
	{
	}
	
	~refCountBase()
	{
	}
	
	bool derefCountBase()
	{
		if(m_refCount == 1) {
			return true;
		}

		--m_refCount;
		return false;
	}

private:
	int m_refCount;
	
};

// reference count base smart ptr.
template <class T> 
class RefCount: public Noncopyable, public refCountBase {
public:
	void deref()
	{
		if(derefCountBase()) {
			delete static_cast<T*>(this);
		}
	}

protected:
	~RefCount()
	{
	}
	
};

}//LDP

using LDP::RefCount;

#endif //refCount.h

